<?php

namespace app\admin\controller;

use think\Db;
use think\Cache;

class Wish extends Common
{
    public function light()
    {
        $where = ['delete_time'=>0];

        $search_name = input('param.search_name');
        if ($search_name) {
            $where['name'] = ['like', '%' . $search_name . '%'];
        }
        
        $data = Db::name('wish_light')
            ->where($where)
            ->order('sort desc, id asc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()])
            ->each(function($item){
                return $item;
            });
        
        $page = $data->render();
        $this->assign('search_name', $search_name);
        $this->assign('data', $data);
        $this->assign('page', $page);
        
        return $this->fetch();
    }
    public function lightadd()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {
                $post['create_time'] = time();
                $res = Db::name('wish_light')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $post['update_time'] = time();
                $res = Db::name('wish_light')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
    
        } else {
            if ($id) {
                $name = Db::name('wish_light')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }

        return $data;
    }
    public function lightdel()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('wish_light')->where(['id'=>$id])->update(['delete_time'=>time()]);
    
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
    public function lists()
    {
        $rejects = Db::name('template_reject_reason')->where('type', 1)->order('sort desc,id asc')->select();
        $this->assign('rejects', json_encode($rejects, JSON_UNESCAPED_UNICODE));

        $where = ['delete_time'=>0];

        $state = input('state/d');
        
        if ($state === null) {
            $state = 0;
        }
        $where['state'] = $state;
        $this->assign('state', $state);

        $search_typeid = input('param.search_typeid');
        $search_name = input('param.search_name');
        if ($search_name) {
            if (in_array($search_typeid, ['user_id'])) {
                $where[''.$search_typeid] = $search_name;
            }else {
                $where['content'] = ['like', '%' . $search_name . '%'];
            }
        }
        $order = input('order') ? input('order') : 'desc';
        if ($order == 1) {
            $order = 'desc';
        } else if ($order == 2) {
            $order = 'asc';
        }
        $lights = Db::name('wish_light')->column('name,image','id');
        $data = Db::name('wish')
            ->where($where)
            ->order('id ' . $order)
            ->paginate(_getCachePages(), false, ['query' => request()->param()])
            ->each(function($item) use ($lights){
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                $item['audit_time'] = date('Y-m-d H:i:s', $item['audit_time']);
                $item['light'] = $lights[$item['wish_light_id']];
                return $item;
            });
        
        $page = $data->render();
        $this->assign('search_typeid', $search_typeid);
        $this->assign('search_name', $search_name);
        $this->assign('data', $data);
        $this->assign('page', $page);
        
        return $this->fetch();
    }


    public function shenheAll()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $type = input('type');
            if (input('?post.delAll')) {
                $post = $post['delAll'];
                $db = Db::name('wish');
                $timenow = time();
                foreach ($post as $k => $v) {
                    if ($type == 'del') {
                        $res = Db::name('wish')->where(['id'=>$v])->find();
                        $dbres = $db->where(['id'=>$v])->update(['delete_time'=>$timenow]);
                    } else {
                        $dy = Db::name('wish')->where(['id'=>$v])->find();
                        $update = [
                            'state'    =>  $type,
                            'audit_time'=>$timenow
                        ];
                        $res = $db->where('id', $v)->update($update);
                    }
                }
                if ($res) {
                    $data = ['ret' => '操作成功'];
                } else {
                    $data = ['err' => '操作失败'];
                }
            } else {
                $data = ['err' => '操作失败，未选中数据'];
            }
        } else {
            $data = ['err' => '提交类型错误'];
        }
        return $data;
    }


    public function authadd()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if ($id) {
                $dy = db('wish')->where('id', $id)->find();
                $update = ['state' => $post['shenhe'], 'reason' => $post['reason'], 'audit_time' => time()];
                $db = db('wish')->where('id', $id)->update($update);

                if ($db) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            } else {
                $data = ["err" => '参数缺失'];
            }
        }
        return $data;
    }
    public function listdel()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('wish')->where(['id'=>$id])->update(['delete_time'=>time()]);
    
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
}